var uid, ws, doctorid, doctorname, doctoravatar, videoprice, overPay = '0', money, wenzhen;
var balance;	//余额支付
document.addEventListener('plusready', function(){
	uid = plus.storage.getItem('uid');
	balance = plus.storage.getItem('money');
	ws = plus.webview.currentWebview();
	doctorid = ws.doctorid;//医生id
	doctorname = ws.doctorname;//医生名
	doctoravatar = ws.doctoravatar;//医生头像
	money = ws.videoprice;//视频价格
	console.log(doctorid+" "+doctorname+" "+doctoravatar+" "+money);
	$('.name').text(doctorname);
	$('.userPic').attr('src', getAvatar(doctoravatar));
	plus.nativeUI.closeWaiting();
	//点击确定预约
	$('.rz-btn').click(function(){
		if(overPay == '1'){
			//判断是否支付
			goNewPage('find-doctor-time-video-ing.html', {doctorid:doctorid,doctorname:doctorname, doctoravatar:doctoravatar, money:money, wenzhen:wenzhen, spage:ws.id})
		}else{
			toast('您还未支付，请先支付');
		}
	})
	//选择支付方式
	$('#picture-btn').on('tap', function(){
		$('.zhifzzao').show();
    	$('.zhiftc').slideDown()
	})
	//支付方式
	$('.zhiftc div').on('tap', function(){
		console.log($(this).index());
		switch($(this).index()){
			case 1://点击微信支付
			ptype = 'wxpay'; 
			payment1();
			break;
			case 2://点击支付宝支付
			ptype = 'alipay'; 
			payment1();
			break;
			case 3://点击银联支付
			toast('待接口提供');return;
			break;
			case 4://点击余额支付
			console.log(money+ " "+balance);
			if(parseFloat(balance) < parseFloat(money)){
				toast('您的余额不足请及时充值');
			}else{
				ReBalance();
			}
			break;
		}
	})
},false)
//减少账户余额
function ReBalance(){
	$.ajax({
		type:"get",
		url:apiRoot+"/home/income/Decbalance",
		data :{
			uid :uid,
			money :money
		},
		success:function(data){
			console.log(data);
			var str = parseFloat(balance) - parseFloat(money);
			plus.storage.setItem('money', str+'');
			overPay = '1';
			source();
			payOrder();
			$('.paymoney').text(money+'元');
		},
		error :function(e){
			console.log(JSON.stringify(e));
		}
	});
}
//支付
function payment1(){
	//预设订单Id ——> 用户Id+当前时间戳
	var orderid = uid+new Date().getTime();
	if(w){return;}//检查是否请求订单中
	console.log(orderid+" "+money+" "+ptype); 
	if(ptype == 'alipay'){
//		console.log(webRoot + '/pay/Alipay.php?orderid=' + orderid + '&price='+money);return;
		url = webRoot + '/pay/Alipay.php?orderid=' + orderid + '&price='+money;
	}else if(ptype == 'wxpay'){
//		console.log(webRoot + '/pay/index.php?orderid=' + orderid + '&price='+money);return;
		url = webRoot + '/pay/index.php?orderid=' + orderid + '&price='+money+'&type=1';
	}
	w = plus.nativeUI.showWaiting('支付中...',{width:'80px',height:'80px',background:'rgba(0,0,0,0.3)'});
	// 请求支付订单
	var xhr = new XMLHttpRequest();
	xhr.onreadystatechange = function() {
		switch (xhr.readyState) {
			case 4:
			w.close(); 
			w = null;
			if (xhr.status == 200) { 
				var order = xhr.responseText;
				console.log(JSON.stringify(pays[ptype]));
				console.log(order);
				plus.payment.request(pays[ptype], order, function(result) { 
					//对订单进行操作
					source();
					payOrder();
					$('.paymoney').text(money+'元');
					overPay = '1';
				}, function(e) {
					console.log("提示", null, "支付失败");
					console.log("["+e.code+"]："+e.message);
				});
			} else { 
				plus.nativeUI.toast("获取订单信息失败！", null, "捐赠");
			}
			break;
		default:
			break;
		}
	}
	xhr.open('GET', url);
	xhr.send();
}
//添加收入支出记录
function source(){
	$.ajax({
		type:"get",
		url:apiRoot+"?m=home&c=income&a=recharge",
		data :{
			payer :uid,
			earner :doctorid,
			source :'视频咨询',
			money :money,
			tid :77//支出
		},
		dataType :'json',
		success :function(data){
			console.log(JSON.stringify(data));
			if(data > 0){
				toast('支付成功');
			}else{
				toast('支付失败');
			}
		},
		error :function(e){
			errortoast();
		}
	});
}	
//增加一条问诊记录
function payOrder(){
	$.ajax({
		type:"get",
		url:apiRoot+"?m=home&c=chat&a=sendMessage",
		data :{
			sendid :uid,
			receiveid :doctorid,
			status :'0',
			type :'3',
			tid :80
		},
		success :function(data){
			wenzhen = data;
			console.log(data);
			if(data > 0){
				goNewPage('find-doctor-time-video-ing.html',{doctorid:doctorid,doctorname:doctorname, doctoravatar:doctoravatar, money:money, wenzhen:wenzhen, spage:ws.id})
			}
		},
		error :function(e){
			errortoast();
		}
	});
}
